export default async function (Vue, {
  config
}) {
  if (window.location.href && window.location.href.indexOf('sb-pc') > -1) {
    // eslint-disable-next-line no-undef
    return new Promise((resolve, reject) => {
      Vue.prototype.$pcSend = function (name, data) {
        window.top.postMessage({
          name,
          data
        }, '*')
      }
      // 事件库
      const fns = {
        'pc-edit': [
          function () {
            Vue.prototype.$PC = true
            resolve(true)
          }
        ]
      }
      Vue.prototype.$pcOn = function (name, fn) {
        if (fns[name] === undefined) {
          fns[name] = []
        }
        if (fns[name].indexOf(fn) === -1) {
          fns[name].push(fn)
        }
      }
      Vue.prototype.$pcOff = function (name, fn) {
        if (fns[name] !== undefined) {
          if (fn === undefined) {
            delete fns[name]
          } else {
            fns[name].splice(fns[name].indexOf(fn), 1)
          }
        }
      }
      window.addEventListener('message', function (e) {
        if (e.origin !== config.pl) {
          return false
        }
        if (!e.data || !e.data.name || fns[e.data.name] === undefined) {
          return false
        }
        fns[e.data.name].forEach(item => {
          item(e.data.data)
        })
      }, false)
      Vue.prototype.$pcSend('ready')
    })
  }
}
